//
//  SHTabBar.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/19.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
protocol SHTabBarDelegate:NSObjectProtocol{
    
    func composeButtonClick()
}

class SHTabBar: UITabBar {

    weak var shDelegate:SHTabBarDelegate?
    
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听方法
    func buttonClick(){
        
        closure?()
    }
    
   private func setupUI(){
        
        addSubview(composeButton)
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = frame.width * 0.2
        
        var index: CGFloat = 0
        
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.origin.x = w * index
                
                value.frame.size.width = w
                
                index++
                
                if index == 2{
                    
                    index++
                }
            }
        }
        
        composeButton.center.x = frame.size.width * 0.5
        
        composeButton.center.y = frame.size.height * 0.5
    }
    
    
    lazy var composeButton: UIButton = {
        
        let compose = UIButton()
        
        compose.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        compose.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        compose.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        compose.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        compose.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        compose.sizeToFit()
        
        return compose
        
    }()
    

}
